//
//  NewCountButton.m
//  SinaWebo
//
//  Created by wwj on 16/1/21.
//  Copyright © 2016年 亦心. All rights reserved.
//

#import "NewCountButton.h"

@implementation NewCountButton

+(instancetype)newCountButton{
    return [[self alloc] init];
}

+(instancetype)newCountButtonWithCount:(int)count{
    NewCountButton *btn = [self newCountButton];
    btn.count = count;
    return btn;
}

- (instancetype)init{
    self = [super init];
    if (self) {
        //设置图片和文字
        self.userInteractionEnabled = NO;
        [self setBackgroundColor:[UIColor orangeColor]];
        [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.titleLabel.font = [UIFont systemFontOfSize:14];
    }
    return self;
}

-(void)setCount:(int)count{
    _count = count;
    if (count) {
        NSString *title = [NSString stringWithFormat:@"共有%d条新的微博", count];
        [self setTitle:title forState:UIControlStateNormal];
    } else {
        [self setTitle:@"没有新的微博数据" forState:UIControlStateNormal];
    }
}

-(void)setFrame:(CGRect)frame{
    CGFloat btnH = 30;
    CGFloat btnY = 64 - btnH;
    CGFloat btnX = 0;
    CGFloat btnW = kScreenW - 2 * btnX;
    frame = CGRectMake(btnX, btnY, btnW, btnH);
    [super setFrame:frame];
}

-(void)show{
    //通过动画移动按钮(按钮向下移动)
    [UIView animateWithDuration:0.7 animations:^{
        self.transform = CGAffineTransformMakeTranslation(0, self.frame.size.height);
    } completion:^(BOOL finished) { // 向下移动的动画执行完毕后
        // 建议:尽量使用animateWithDuration, 不要使用animateKeyframesWithDuration
        [UIView animateWithDuration:0.7 delay:1.0 options:UIViewAnimationOptionCurveLinear animations:^{
            self.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            // 将btn从内存中移除
            [self removeFromSuperview];
        }];
        
    }];
}

@end
